import React, {Component} from 'react';
import {Button, message, Space, Table} from "antd";
import {Link} from "umi";
import {PageContainer} from "@ant-design/pro-components";
import {QuerySubMenus} from "@/services/School/SchoolMenuApi";
import {MergeMenu, ShowError} from "@/utils/format";
import {ServerMessages} from "@/services/ServerMessages";

class SocialSupport extends Component {
  state = {
    loading: true,
    data: []
  }

  componentDidMount() {
    // console.log('@请求')
    // const userInfo = getUserInfo()
    // console.log(userInfo.token)
    // request('http://192.168.31.34:3007/api/index/disci-cons/progress/', {
    //   method: 'POST',
    //   headers: {
    //     'Content-Type': 'application/json;charset=utf-8',
    //     'Authorization': userInfo.token
    //   },
    //   data: {"id":["1-1-1", "1-1-2","1-1-3","1-1-4"]},
    //   timeout: 3000,
    // }).then(response => {
    //   console.log('@response', response)
    //   let data = [
    //     {
    //       key: '1-1-1',
    //       name: '学科建设进展情况写实',
    //       circle: '每季度一次',
    //       target: '学科填报',
    //       flag: 0,
    //       to: '/disci-cons/progress/progress-situation',
    //     },
    //     {
    //       key: '1-1-2',
    //       name: '学科评估情况',
    //       circle: '五年一次',
    //       target: '学科填报',
    //       flag: 0,
    //       to: '/disci-cons/progress/disci-eval-situation',
    //     },
    //     {
    //       key: '1-1-3',
    //       name: '学科影响力情况',
    //       circle: '一年一次',
    //       target: '学科填报',
    //       flag: 0,
    //       to: '/disci-cons/progress/disci-influence',
    //     },
    //     {
    //       key: '1-1-4',
    //       name: '本学科建设经费数额',
    //       circle: '一年一次',
    //       target: '学科填报',
    //       flag: 0,
    //       to: '',
    //     },
    //   ]
    //   // 将 response 的 flag 映射到 data 的 flag 上
    //   for (let i = 0; i < data.length; i++) {
    //     for (let j = 0; j < response.length; j++) {
    //       console.log(data[i].key, response[j].id)
    //       if (data[i].key === response[j].id) {
    //         data[i].flag = response[j].flag
    //         break
    //       }
    //     }
    //   }
    //   this.setState({loading: false, data})
    //   console.log(data)
    // }, () => {
    //   this.setState({loading: false})
    //   ShowError('服务器故障')
    // })


    let data = [
      {
        key: '5_2_1_1',
        name: '国家级产教融合平台建设数',
        circle: '每年一次',
        target: '学科填报',
        flag: 1,
        to: '/social-services/social-support/nation-counts',
      },
      {
        key: '5_2_1_2',
        name: '省部级产教融合平台建设数',
        circle: '每年一次',
        target: '学科填报',
        flag: 1,
        to: '/social-services/social-support/province-counts',
      },
      {
        key: '5_2_2_1',
        name: '国家级咨政研究情况',
        circle: '每季度一次',
        target: '学科填报',
        flag: 1,
        to: '/social-services/social-support/consultative-nation-counts',
      },
      {
        key: '5_2_2_2',
        name: '省部级咨政研究情况',
        circle: '每季度一次',
        target: '学科填报',
        flag: 1,
        to: '/social-services/social-support/consultative-province-counts',
      },
    ]
    // 将 response 的 flag 映射到 data 的 flag 上
    // this.setState({loading: false, data})

    QuerySubMenus('5_2').then(response => {
      console.log("@返回数据", response)
      data = MergeMenu(data, response)
      console.log(data)
      this.setState({loading: false, data})
    }, () => {
      ShowError(ServerMessages.error_message)
    })

  }

  render() {

    const columns = [
      {
        title: '监测点',
        dataIndex: 'name',
        key: 'name',
        render: text => <b>{text}</b>,
      },
      {
        title: '填报周期',
        dataIndex: 'circle',
        key: 'circle',
        render: text => <b>{text}</b>
      },
      {
        title: '填报对象',
        dataIndex: 'target',
        key: 'target',
      },
      {
        title: '操作',
        key: 'action',
        render: (_, record) => (
          <Space size="middle">
            {record.is_period === 0 && (
              <Button type="primary" disabled>未到填报时间</Button>
            )}
            {record.is_period === 1 && record.is_filled === 1 && (
              <>
              <Button type="primary" disabled>已填报</Button>
              <Link to={record.to+"?flag=display"}><Button type="default">查看</Button></Link>
              </>
              )}
            {record.is_period === 1 && record.is_filled === 0 && (
              <Link to={record.to}><Button type="primary">填报</Button></Link>
            )}


          </Space>
        ),
      },
    ];

    return (
      <div>
        <PageContainer header={{
          title: '请选择填报项目',
          breadcrumb: {},
        }}
        >
          <Table loading={this.state.loading} columns={columns} dataSource={this.state.data} pagination={{pageSize: 100}} />
        </PageContainer>
      </div>
    );
  }
}

export default SocialSupport;
